/****************************************************************

kernel/main.c
Entry point for AWOS Kernel

Copyright (c) 2006 Andrew Wilcox. All rights reserved.

Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.

3. The end-user documentation included with the redistribution,
if any, must include the following acknowledgment:

"This product includes software developed by Andrew Wilcox for
AWOS. (http://code.google.com/p/awos)."

Alternately, this acknowledgment may appear in the software
itself, if and wherever such third-party acknowledgments normally
appear.

4. The names "AWOS" and "Andrew Wilcox" must not be used to
endorse or promote products derived from this software without
prior written permission. For written permission, please contact
Andrew.R.Wilcox@Gmail.com.

5. Products derived from this software may not be called "AWOS"
 nor may "AWOS" appear in their name, without prior written
 permission of Andrew Wilcox.

THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL ANDREW WILCOX BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.

****************************************************************/

/*! \file main.c
 * kernel entry point and misc global vars
 */

#include <asm.h>
#include <kernel.h>
#include <timeout.h>
#include <string.h>
#include <driver/hdd.h>

/**global boolean defining whether debugging is turned on**/
BOOL debug;
/**global boolean defining whether a log is available to write to**/
BOOL log;
/**global integer defining the major version number**/
int major = 0;
/**global integer defining the minor version number**/
int minor = 0;
/**global integer defining the build number**/
int build = 9;
/**global CPU info structure**/
struct KiCpuInfo cpuinfo;
/**global hard disk layout structure**/
struct KiDiskLayout *dlayout;

void KiCpuId(struct KiCpuInfo *);
void KsGdtIdtIsr();
struct KiDiskLayout *KsHardDisk();
void KsKeyb();
void KsMemory(int);
BOOL KsPaging(int);
BOOL KsPCI();
void KsTimer();
void KsVideo();
int KsDetectMP();

/*! \fn void k_main(multiboot_info_t *boot_info)
 * \brief C kernel entry point
 * \note yes I know the real entry point is in assembly.  doxygen will not read my assembly, so oh well.
 */
void k_main(multiboot_info_t *boot_info)
{	
	if(strstr((char *)boot_info->cmdline, "debug"))
		debug = TRUE;
	dprintf("booting AWOS %i.%i.%i", major, minor, build);
	KsGdtIdtIsr();
	dprintf(".");
	sti();
	dprintf(".");
	KsVideo();
	int processors = KsDetectMP();
	dprintf(".\n");
	dprintf("%d\n", strcmp("GenuineIntel", "GenuineIntel"));
	printf("AWOS Build %i - Operating on %i processo%s, with %iM memory.\n", build, \
	       processors, ((processors == 1) ? "r" : "rs"), (int)(boot_info->mem_upper / 1024));
	KiCpuId(&cpuinfo);
	printf(".");
	KsTimer();
	printf(".");
	KsKeyb();
	printf(".");
	if(!KsPaging(boot_info->mem_upper))
		dprintf("KiPaging returned FALSE -- paging system disabled");
	printf(".");
	KsMemory(boot_info->mem_upper);	
	printf(".");
	_timeout_init();
	dlayout = KsHardDisk();
	/*if(KsHardDisk())
	{
		log = TRUE;
		lprintf("!");
		lprintf("kernel: this is AWOS %i.%i.%i\n", major, minor, build);
		lprintf("kernel: mp support is %s\n", ((processors == 1) ? "disabled" : "enabled"));
		lprintf("kernel: %i kb memory.", boot_info->mem_upper);
	} else {
		log = FALSE;
		
	}*/
	dprintf(".\n");
	if(KsPCI() == FALSE) lprintf("pci: No PCI bus found.\n");
	else lprintf("pci: PCI bus found.\n");
	printf("\nAWOS Command Interpreter (MS-DOS edition)\nCopyright (c) 2006 Andrew Wilcox\n\n");
	halt();
}
